<?php

namespace app\common\model\work;

use think\Model;


class Service extends Model
{

    

    

    // 表名
    protected $name = 'work_service';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_data_text',
        'servicetime_text',
        'applytime_text',
        'statementtime_text',
        'contract_status_text',
        'service_type_text',
        'warranty_time_text',
        'present_time_text',
        'status_text'
    ];
    

    
    public function getTypeDataList()
    {
        return ['0' => __('Type_data 0'), '1' => __('Type_data 1')];
    }

    public function getContractStatusList()
    {
        return ['0' => __('Contract_status 0'), '1' => __('Contract_status 1'), '2' => __('Contract_status 2')];
    }

    public function getServiceTypeList()
    {
        return ['0' => __('Service_type 0'), '1' => __('Service_type 1'), '2' => __('Service_type 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getTypeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_data']) ? $data['type_data'] : '');
        $list = $this->getTypeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getServicetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['servicetime']) ? $data['servicetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getApplytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['applytime']) ? $data['applytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatementtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['statementtime']) ? $data['statementtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getContractStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['contract_status']) ? $data['contract_status'] : '');
        $list = $this->getContractStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getServiceTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['service_type']) ? $data['service_type'] : '');
        $list = $this->getServiceTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWarrantyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['warranty_time']) ? $data['warranty_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPresentTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['present_time']) ? $data['present_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setServicetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setApplytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStatementtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setWarrantyTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPresentTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function warranty()
    {
        return $this->belongsTo('Warranty', 'warranty_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function workflow()
    {
        return $this->belongsTo('Workflow', 'service_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
